<?php

/**
 * 每天定时计算 品牌 商品标签 商品占比  算出标签品牌指数
 */

ini_set('display_errors', 1);
ini_set('memory_limit', '1024M');
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\cache\brand\BrandScoreCache;
use Doraemon\cache\Wiki\SearchBrandScore;

$where = [
    'basic_state' => mWikiBasic::STATE_ONLINE,
    'brand_id[>]' => 0,
];
$basicList = mWikiBasic::selectByWhere($where);
$brandBasicIds = [];
$brandTagBasicIds = [];
foreach ($basicList as $row) {
    $styleArr = empty($row['style']) ? [] : explode(',', $row['style']);
    $materialArr = empty($row['material']) ? [] : explode(',', $row['material']);
    $spaceArr = empty($row['space']) ? [] : explode(',', $row['space']);
    $tagArr = array_merge(array_merge($styleArr, $materialArr), $spaceArr);
    $tagArr = array_filter($tagArr);
    $brandBasicIds[$row['brand_id']][] = $row['id'];
    if ($tagArr) {
        foreach ($tagArr as $tag) {
            $brandTagBasicIds[$row['brand_id']][$tag][] = $row['id'];
        }
    }
}
SearchBrandScore::delItem(SearchBrandScore::TAG_BRAND_SCORE);
foreach ($brandTagBasicIds as $brandId => $tagBasicIdList) {
    $brandWikiCount = count($brandBasicIds[$brandId]);
    $brandScore = BrandScoreCache::getBrandScore($brandId);
    $brandScore = empty($brandScore) ? 1 : $brandScore;
    var_dump('brand Id:' . $brandId . '的品牌指数：' . $brandScore);
    foreach ($tagBasicIdList as $tagNmae => $basicIds) {
        $basicIds = array_unique(array_filter($basicIds));
        $scale = (count($basicIds) / $brandWikiCount);//商品占比
        $scale = $brandScore * (1 + $scale);
        var_dump('标签品牌指数：' . $brandId . '_' . $tagNmae . ":" . $scale);
        SearchBrandScore::setTagBrandScoreById($brandId . '_' . $tagNmae, $scale);
    }
}